﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022 北京超维景生物科技有限公司 保留所有权利。
 * CLR版本：4.0.30319.42000
 * 文件名：Kernel
 * 
 * 创建者：huangyang
 * 电子邮箱：huangyang@tvscope.cn
 * 创建时间：2023/2/8 14:25:20
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ImageK.Plugin.Frame;

namespace ImageK.Macros
{
    /// <summary>
    /// This is the recursive descent parser/interpreter for the ImageJ macro language.
    /// </summary>
    public class Interpreter
    {
        static volatile Interpreter instance, previousInstance;
        public static bool batchMode;
        static ArrayList imageTable; // images opened in batch mode
        static ArrayList imageActivations; // images ordered by activation time

        volatile bool done;
        // Program pgm;

        Functions func;

        static bool tempShowMode;

        public string eval(string code)
        {
            throw new NotImplementedException();
        }

        public static int getBatchModeImageCount()
        {
            //todo:
            return 0;
        }

        public static ImagePlus getBatchModeImage(int id)
        {
            if (!batchMode || imageTable == null)
                return null;
            foreach (ImagePlus en in Interpreter.imageTable)
            {
                ImagePlus imp = (ImagePlus)en;
                if (id == imp.getID())
                    return imp;
            }
            return null;
        }

        public static Interpreter getInstance()
        {
            return instance;
        }

        public static bool isBatchMode()
        {
            return batchMode && !tempShowMode;
        }

        public static void activateImage(ImagePlus imp)
        {
            if (imageTable != null && imp != null)
            {
                if (imageActivations == null)
                    imageActivations = new ArrayList();
                imageActivations.Remove(imp);
                imageActivations.Add(imp);
            }
        }

        public static int[] getBatchModeImageIDs()
        {
            if (!batchMode || imageTable == null)
                return new int[0];
            int n = imageTable.Count;
            int[] imageIDs = new int[n];
            for (int i = 0; i < n; i++)
            {
                ImagePlus imp = (ImagePlus)imageTable[i];
                imageIDs[i] = imp.getID();
            }
            return imageIDs;
        }

        public static void addBatchModeImage(ImagePlus imp)
        {
            if (!batchMode || imp == null) return;
            if (imageTable == null)
                imageTable = new ArrayList();
            imageTable.Add(imp);
            activateImage(imp);
        }

        public static ImagePlus getLastBatchModeImage()
        {
            return null;
            // if (!batchMode || imageTable == null)
            //     return null;
            // ImagePlus imp2 = null;
            // try
            // {
            //     int size = imageTable.size();
            //     if (size == 0)
            //         return null;
            //     if (imageActivations != null && imageActivations.size() > 0)
            //         imp2 = (ImagePlus)imageActivations.get(imageActivations.size() - 1);
            //     if (imp2 == null)
            //         imp2 = (ImagePlus)imageTable.get(size - 1);
            // }
            // catch (Exception e) { }
            // return imp2;
        }

        /** Returns the batch mode RoiManager instance. */
        public static RoiManager getBatchModeRoiManager()
        {
            Interpreter interp = getInstance();
            if (interp != null && isBatchMode() && RoiManager.getRawInstance() == null)
            {
                if (interp.func.roiManager == null)
                    interp.func.roiManager = new RoiManager(true);
                return interp.func.roiManager;
            }
            else
                return null;
        }

        public static void removeBatchModeImage(ImagePlus imp)
        {
            if (imageTable != null && imp != null)
            {
                int index = imageTable.IndexOf(imp);
                if (index != -1)
                {
                    imageTable.Remove(index);
                    imageActivations.Remove(imp);
                    WindowManager.setTempCurrentImage(getLastBatchModeImage());
                }
            }
        }
    }
}
